package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    //按条件分页查询
    @GetMapping("/setmeal/page")       //传入默认值
    public ResultInfo page(@RequestParam(value = "page", defaultValue = "0") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           String name) {


        Page<Setmeal> page = setmealService.findByPage(pageNum, pageSize, name);

        return ResultInfo.success(page);
    }

    //菜品回显
    @GetMapping("/dish/list")
    public ResultInfo list(Long categoryId ,String name) {

        List<Dish> list= setmealService.list(categoryId,name);

        return ResultInfo.success(list);
    }

    //新增菜品
    @CacheEvict(value = "setmeal",key = "setmeal.categoryId")
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){

            setmealService.save(setmeal);

        return ResultInfo.success();
    }


    //套餐回显
    @GetMapping("/setmeal/{id}")
    public ResultInfo setmealFindByid(@PathVariable long id){

           Setmeal setmeal= setmealService.setmealFindByid(id);

        return ResultInfo.success(setmeal);
    }
      //修改菜品
      @CacheEvict(value = "setmeal",allEntries = true)
    @PutMapping("/setmeal")
    public ResultInfo update(@RequestBody Setmeal setmeal){
        setmealService.update(setmeal);

        return  ResultInfo.success();

    }
    //起售 停售

    @PostMapping("/setmeal/status/{status}")
    @CacheEvict(value = "setmeal",allEntries = true)
    public ResultInfo update(@PathVariable("status") Integer status, @RequestParam List<Long> ids) {

         setmealService.statuSetmeal(status,ids);

        return ResultInfo.success();
    }


    //菜品删除
    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping("/setmeal")
    public ResultInfo deleSetmealByid(@RequestParam List<Long> ids) {

        setmealService.deleByid(ids);
        return ResultInfo.success();
    }


}
